/**
 * Race Scene
 * @author David Ronai
 */

var RaceScene = function Race( camera, scene, render )
{
	Abstract3DScene.call( this, camera, scene, render );
	
	this.spectators = [];
	this.sprinters = [];
	this.position = new THREE.Vector3(2500,0,0);
	this.raceScene = new THREE.Object3D();
	this.racePlaying = false;
	this.finish = 0;
	
	this.init = function init()
	{
		$("#avatarInfo").attr("src",user.profile_image_url);
		
		instanceRace = this;
		scene.add(this.raceScene);
		$("#race").fadeIn('slow');
		
		addSpotlight({color:0xFFFFFF, x:8000, y:3000, z:2000, intensity:1, targetX:2500, fov:3000, far:10000, visible:0});
		addSpotlight({color:0xFFFFFF, x:-8000, y:5000, z:-2000, intensity:1, targetX:2500, fov:3000, far:10000, visible:0});
		
		var m = userMobile = new TO.Mobile(imageUser);
		m.scale.x = m.scale.y = m.scale.z = 1; 
		
		m.position.x = -10;
		m.position.z = 290;
		m.position.y = 0;
		m.rotation.y = Math.PI/2;
		
		this.sprinters.push( m );
		this.raceScene.add( m );
		
		// Sprint
		this.decor = modelLoader.get("decor_sprint");
		this.decor.position.x = 1400;
		this.decor.position.y = -50;
		this.decor.position.z = 15;
		this.raceScene.add( this.decor );
		
		//spectators
		var followers = RessourceManager.getInstance().followers;
		
		//for( var i = 0; i < followers.length; i++)
		//{
			//followers[i].mobile = new TO.Mobile(followers[i].image, followers[i],true);
			//var m = followers[i].mobile;
			//this.spectators.push(m);
			//this.raceScene.add(m);
			//m.position.x = i * 290 + Math.random()*450;
			//m.position.z = -400-Math.random()*300;
		//}
		
		//for( var i = 0; i < followers.length; i++)
		//{
			//followers[i].mobile = new TO.Mobile(followers[i].image, followers[i],true);
			//var m = followers[i].mobile;
			//this.spectators.push(m);
			//this.raceScene.add(m);
			//m.position.x = (followers.length-i) * 250 + Math.random()*200;
			//m.position.z = 550-Math.random()*200;
			//m.rotation.y = Math.PI;
			//m.updateMatrix();
		//}
		
		var friends = RessourceManager.getInstance().friends;
		
		for( var i = 0; i < 3; i++)
		{
			if(friends[i].image==null){
				friends[i].image = document.createElement("img");
				friends[i].image.source = 'img/particle.png';
			}
			friends[i].mobile = new TO.Mobile(friends[i].image, friends[i]);
			var m = friends[i].mobile;
			this.sprinters.push(m);
			this.raceScene.add(m);
			m.position.x = 0;
			m.position.z = 100-175*i;
			m.position.y = 0;
			m.rotation.y = Math.PI/2;
			m.updateMatrix();
		}
		
		//CAMERA VU GLOBALE DU HAUT
		camera.position.x = 3500;
		camera.position.y = 6000;
		camera.position.z = 500;
		camera.lookAt(2500,0,0);
		
		TweenLite.to(camera.position, 5, { x:3500, y:3500, z:-500, ease:Linear.easeNone});
		TweenLite.to(instanceRace.position, 5, { x:2500, y:0, z:0, ease:Linear.easeNone, onComplete:instanceRace.camera2});
	}
	
	this.camera2 = function()
	{
		camera.position.x = -900;
		camera.position.y = 1000;
		camera.position.z = -400;
		camera.lookAt(0,50,0);
		instanceRace.position.x = 0;
		instanceRace.position.y = 50;
		instanceRace.position.z = 0;
		
		TweenLite.to(camera.position, 3, {x:-900, y:900, z:400, ease:Linear.easeNone, onComplete:instanceRace.camera3});
	}
	
	this.camera3 = function()
	{
		camera.position.x = 200;
		camera.position.y = 50;
		camera.position.z = 400;
		camera.lookAt(0,0,0);
		instanceRace.position.x = 0;
		instanceRace.position.y = 0;
		instanceRace.position.z = 100;
		TweenLite.to(camera.position, 3, { x:200, y:50, z:370, ease:Quad.easeOut});
		TweenLite.to(camera.position, 1.5, { delay:3, x:300, y:600, z:400, ease:Quad.easeInOut, onComplete:instanceRace.startCompteur});
		TweenLite.to(instanceRace.position, 1.5, { delay:3, x:9, y:50, z:0, ease:Linear.easeNone});
	}
	
	this.startCompteur = function()
	{
		var o = {};
		o.x = 0;
		TweenLite.to(o, .3, {x:100, onComplete:instanceRace.startAnimation, onCompleteParams:[true]});
	}
	
	this.startAnimation = function (firstTime){
		if(!firstTime){
			$("#go :first-child").detach();
		}
		if($("#go :first-child").size() > 0){
			$("#go :first-child").fadeIn(150).delay(600).fadeOut(250);
			TweenLite.to($("#go :first-child"), 1,{css:{fontSize:"200px", lineHeight:"200px"}});
			var o = {};
			o.x = 0;
			TweenLite.to(o, 1, {x:100, onComplete:instanceRace.startAnimation, onCompleteParams:[false]});
		} 
		if($("#go div").size() == 1){
			instanceRace.startRun();
		}
	}
	
	this.startRun = function()
	{
		var baseTime = 9;
		var differenceVX = 4;
		var sprinter;

		for(var i=0; i<instanceRace.sprinters.length; i++)
		{
			sprinter = instanceRace.sprinters[i];
			sprinter.initSpeed(9,differenceVX);
		}
		
		instanceRace.racePlaying = true;
	}
	
	this.update = function()
	{
		if(instanceRace.racePlaying){
			var sprinter;
			for(var i=0; i<instanceRace.sprinters.length; i++)
			{
				sprinter = instanceRace.sprinters[i];
				if(sprinter.position.x >= 2900){
					if(!sprinter.poped){
						sprinter.pop();
						this.finish++;
						if(this.finish>=4)
							this.endRace();
					}
				} 
				sprinter.run();
			}
			camera.position.x += (userMobile.position.x+300 - camera.position.x)*.5;
			if(camera.position.x > 2400){
				camera.position.x = 2400;
			}
			instanceRace.position.x = userMobile.position.x;
		} 
		camera.lookAt( instanceRace.position );
	}
	
	this.endRace = function(){
		TweenLite.to(instanceRace.position, 1, { x:1500, y:50, z:-600, ease:Linear.easeNone});
	}
	
	this.dispose = function()
	{
		this.camera = null;
		this.scene = null;
		this.render = null;
		this.state = "";
		this.callback = null;
	}
	
	this.init();
}

RaceScene.prototype = new Abstract3DScene();
RaceScene.prototype.constructor = RaceScene;

var instanceRace;